#include "utils.h"

char **splitFunc(const char *cmd, int *tokensSize) {
    char *delimiters = " \t\n";
    return splitString(cmd, delimiters, tokensSize);
}

char **splitString(const char *str, const char *delimiters, int *tokensSize) {
    // 拷贝一份cmd，避免对传输参数进行修改
    char* strCopy = strdup(str);
    if (!strCopy) {
        perror("strdup");
        return NULL;
    }

    // TODO: 去除cmd首尾的空字符
    *tokensSize = 0;
    // 第一轮调用strtok，获取字符数组长度
    char* temp = strCopy;
    char* token = strtok(temp, delimiters);
    while (token) { 
        printf("%s\n", token);
        token = strtok(NULL, delimiters);
        (*tokensSize)++;
    } 

    // 为字符串数组分配空间
    char **tokens = (char**)malloc((*tokensSize) * sizeof(char*));
    if (!tokens) {
        perror("malloc");
        free(strCopy);
        exit(EXIT_FAILURE);
    }

    // 第二轮调用strtok，存储分割后的字符串
    strcpy(strCopy, str);
    temp = strCopy;
    token = strtok(temp, delimiters);
    for (int i = 0; i < *tokensSize; i++) {
        tokens[i] = strdup(token);
        if (!tokens[i]) {
            perror("strdup");
            for (int j = 0; j < i; j++) {
                free(tokens[j]);
            }
            free(tokens);
            free(strCopy);
            return NULL;
        }
        token = strtok(NULL, delimiters);
    }

    free(strCopy);

    return tokens;
}


// void freeArray(void **array, int size) {
void freeArrayOfPtrs(void **ptrToArray, int arraySize) {
    if (ptrToArray == NULL || *ptrToArray == NULL)
        return;

    for (int i = 0; i < arraySize; i++) {
        if (ptrToArray[i] != NULL) {
            free(ptrToArray[i]);
            ptrToArray[i] = NULL; // Notify that the memory has been freed
        }
    }
    free(ptrToArray);
    ptrToArray = NULL; // Notify that the ptrToArray memory has been freed
}

void freeTokens(char ***tokens, int tokensSize) {
    if (NULL == tokens || NULL == *tokens)
        return;

    for (int j = 0; j < tokensSize; j++) {
        free((*tokens)[j]);
    }
    free(*tokens);

    *tokens = NULL;
}


/*
 * 单元测试函数实现，测试完成后需要删除
 * 创建人：曾爽
 * 创建时间：2024/07/06
 * */

void unitest_splitFunc(void) {
    char *cmd  = "cd /home/tsang/";
    int size = 0;

    char **tokens = splitFunc(cmd, &size);

    printf("Tokens size = %d\n", size);
    printf("Tokens:\n");
    if (size > 0) {
        for(int i = 0; i < size; i++) {
            printf("'%s'\n", tokens[i]);
        }
        freeTokens(&tokens, size);

    } else {
        printf("error\n");
    }    
}

void unitest_splitString(void) {
    char *cmd  = "cd /home/tsang/";
    char *delimeter  = " \t\n/";
    int size = 0;

    char **tokens = splitString(cmd, delimeter, &size);

    printf("Tokens size = %d\n", size);
    printf("Tokens:\n");
    if (size > 0) {
        for(int i = 0; i < size; i++) {
            printf("'%s'\n", tokens[i]);
        }
        // freeTokens(&tokens, size);
        freeArrayOfPtrs((void **)tokens, size);
        if(tokens == NULL) {
            printf("清理成功\n");
        } else {
            printf("清理失败\n");
        }

    } else {
        printf("error\n");
    }    
}


